//
//  ICFileViewController.m
//  ICrEAM
//
//  Created by mac on 16/11/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICFileViewController.h"
#import "FileCollectionCell.h"
#import "FileDirAction.h"
#import "FileDirRespose.h"
#import "ICPDFReadController.h"
#import "MCDownloadManager.h"
#define Content_Height 40
@interface ICFileViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,FileCollectionCelldelegate>

@property(nonatomic,weak)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *fileDirArray;
@property(nonatomic,strong)NSArray *filterArr;
@end

@implementation ICFileViewController
-(NSMutableArray *)fileDirArray{
    if (!_fileDirArray) {
        _fileDirArray=[[NSMutableArray alloc]init];
    }
    return _fileDirArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=BackgroundColor_Gray;
    [self setupNav];
    // Do any additional setup after loading the view.
}
-(void)setupNav{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    view.backgroundColor=RGB(230, 236, 236);
    self.searchTextField.frame=CGRectMake(ScreenWidth-240, 7, 200, 30);
    self.searchTextField.isLowercase=YES;
    self.searchTextField.autocapitalizationType=UITextAutocapitalizationTypeNone;
    self.searchTextField.placeholder=@"请输入文件名";
    [view addSubview:self.searchTextField];
    self.switchArray=@[@"文件查询"];
    self.mySwitch.frame=CGRectMake(0,0,300,44);
    [view addSubview: self.mySwitch];
    [self.view addSubview:view];
    [ self.mySwitch selectIndex:0 animated:YES];
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
//    115/100;
//    CGFloat width=(self.view.frame.size.width-28-12*3)/6.;
    CGFloat height=120;
    CGFloat width=130.;
    layout.itemSize=CGSizeMake(width, height);
    layout.minimumInteritemSpacing=10;
    layout.minimumLineSpacing=10;
    layout.sectionInset=UIEdgeInsetsMake(10, 12, 10, 12);
//    layout.headerReferenceSize=CGSizeZero;
    UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(view.frame), self.view.frame.size.width, self.view.frame.size.height-110) collectionViewLayout:layout];
    collectionView.alwaysBounceVertical=YES;
    collectionView.backgroundColor=RGB(242, 242, 242);
    [collectionView registerNib:[UINib nibWithNibName:@"FileCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"FileCollectionCell"];
  
    collectionView.delegate=self;
    collectionView.dataSource=self;
    [self.view addSubview:collectionView];
    self.collectionView=collectionView;
    if (_isNext) {
        [self.fileDirArray removeAllObjects];
        [self.fileDirArray addObjectsFromArray:self.filePreArray];
        self.filterArr=[NSArray arrayWithArray:self.fileDirArray];
        [self.collectionView reloadData];
        
    }else{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestFileDir];
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.collectionView.mj_header beginRefreshing];
    });
    }
}

#pragma mark 获取文件
-(void)requestFileDir{
    FileDirAction *action=[[FileDirAction alloc]init];
    [self requestDataWithAction:action haveHud:NO showError:NO  success:^(__kindof YTKBaseRequest *request) {
                [self.collectionView.mj_header endRefreshing];
        NSDictionary *file=request.responseJSONObject[@"FILE"];
               [self.fileDirArray removeAllObjects];
        if (file) {
            NSArray *filedirs=file[@"FILES"];
            if (!IsArrEmpty(filedirs)) {
                for (NSDictionary *dic in filedirs) {
                    FileDirRespose *model=[FileDirRespose mj_objectWithKeyValues:dic];
                    [self.fileDirArray addObject:model];
                }
            }
        }
        self.filterArr=[NSArray arrayWithArray:self.fileDirArray];
        [self.collectionView reloadData];
        
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}
#pragma mark 本地文件查询
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
     [self.fileDirArray removeAllObjects];
    if (IsStrEmpty(textField.text)) {
       [self.fileDirArray addObjectsFromArray:self.filterArr];
    }else{
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"fileName CONTAINS %@",textField.text];
    NSArray *arr=[self.filterArr filteredArrayUsingPredicate:predicate];
   
    [self.fileDirArray addObjectsFromArray:arr];
    }
    [self.collectionView reloadData];
    [textField resignFirstResponder];
    return YES;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.fileDirArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FileCollectionCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"FileCollectionCell" forIndexPath:indexPath];
    cell.delegate=self;
    FileDirRespose *model=self.fileDirArray[indexPath.row];
    cell.fileModel=model;
    return cell;
}
-(void)FileCollectionCell:(FileCollectionCell*)cell didDownLoadCompleteIsDir:(BOOL)isDir{
    FileDirRespose *model=cell.fileModel;
    if (isDir) {
        ICFileViewController *ctr=[[ICFileViewController alloc]init];
        ctr.isNext=YES;
        ctr.filePreArray=model.files;
        [self.navigationController pushViewController:ctr animated:YES];
    }else{
         MCDownloadReceipt *receipt = [[MCDownloadManager defaultInstance] downloadReceiptForURL:model.filePath];
    
        ICPDFReadController *pdf=[ICPDFReadController new];
        pdf.documentType=model.documentType;
        pdf.hideSuspendView=YES;
        pdf.hideRightNavItem=YES;
        pdf.title=model.fileName;
        pdf.filePath=receipt.filePath;
        [self.navigationController pushViewController:pdf animated:YES];
      
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


}
@end
